// Header files and standard libraries
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
//~ #ifdef __WINDOWS__
	//~ #include <string>
	//~ #include <GL/glut.h>
//~ #else
	#include <string.h>
	#include <GL/freeglut.h>
//~ #endif

using std::string;
using std::ifstream;
using std::cout;
using std::endl;
using std::vector;


// Define window
#define WINDOW_XS 500
#define WINDOW_YS 500
#define WINDOW_NAME "3D Image Viewer"
#define PI 3.14159265

// Define point structure
typedef struct pt
{
	GLdouble x, y, z;
	GLdouble r, g, b;
}MyPoint;

vector<MyPoint> vertices, verticesFromOutput;
bool autoRotate = false, multiFiles = false, displayOutput = false,
one = false, two = false, three = false, four = false, five = false, six = true;
double angle = 0.0;
double center_x, center_y, center_z, rad, axis_a, axis_b, axis_c, alpha, beta, theta;
int numVertices;
double near_x = -1000.0, far_x = 1000.0, near_y = -1000.0, far_y = 1000.0, near_z = -5000.0, far_z = 5000.0;
double camera_angle_h = 0;
double camera_angle_v = 0;

int drag_x_origin;
int drag_y_origin;
int dragging = 0;


// Function prototypes
void display_func(void);
void keyboard_func(unsigned char c, int x, int y);
void mouse_func(int button, int state, int x, int y);
void mouse_move(int x, int y);
void reshape_handler(int width, int height);
void init_setup(int width, int height, char *windowName);
bool readFile(string fileName, vector<MyPoint> &verts);

// Begin main function
int main(int argc, char **argv)
{
	string fileName1, fileName2;
	glutInit(&argc, argv);

	init_setup(WINDOW_XS, WINDOW_YS, (char *)WINDOW_NAME);
	
	if(argc >= 3)
	{
		fileName1 = argv[1];
		fileName2 = argv[2];
		multiFiles = true;
	}
	else if(argc == 2)
	{
		fileName1 = argv[1];
		multiFiles = false;
	}
	else
		fileName1 = "inputPixels";
		
	
	if(!readFile(fileName1, vertices))
	{
		cout << "Error reading data from file: " << fileName1 << endl;
		return 1;
	}
	
	if(multiFiles)
	{
		if(!readFile(fileName2, verticesFromOutput))
		{
			cout << "Error reading data from file: " << fileName2 << endl;
			return 1;
		}
	}

	glutKeyboardFunc(keyboard_func);
	glutDisplayFunc(display_func);
	glutMouseFunc(mouse_func);
	glutMotionFunc(mouse_move);
	glutIdleFunc(display_func);
	glutMainLoop();

	return 1;
}// End main function

// Begin display function
void display_func(void)
{
	double tempA, tempB, tempC;

	glMatrixMode(GL_MODELVIEW);
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


	glLoadIdentity();

	glEnable(GL_DEPTH_TEST);
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	gluLookAt(0.0, 0.0, 0.0, 500, 500, -1000, 0.0, 1.0, 0.0);
	glTranslatef(center_x, center_y, center_z);
	//glRotatef(angle,0.0f,1.0f,0.0f);
	glRotated(camera_angle_v, 1.0, 0.0, 0.0);
    glRotated(camera_angle_h, 0.0, 1.0, 0.0);

	glTranslatef(-center_x, -center_y, -center_z);
	
	glColor3f(1.0, 1.0, 0.0);
	glBegin(GL_LINES);
	glVertex3f(-500, 0, 0);
	glVertex3f(500, 0, 0);
	glEnd();
	glColor3f(1.0, 0.6, 0.0);
	glBegin(GL_LINES);
	glVertex3f(0, -500, 0);
	glVertex3f(0, 500, 0);
	glEnd();
	glColor3f(1.0, 0.3, 0.0);
	glBegin(GL_LINES);
	glVertex3f(0, 0, -500);
	glVertex3f(0, 0, 500);
	glEnd();
	
	
	glPointSize(5.0);
	glBegin(GL_POINTS);
	glColor3f(1.0, 0.3, 0.6);
	glVertex3f(500, 0, 0);
	glVertex3f(0, 500, 0);
	glVertex3f(0, 0, 500);
	glEnd();
	
	
	glPointSize(1.0);
	if(displayOutput)
	{
		glColor3f(0.0, 0.0, 1.0);
		glBegin(GL_POINTS);
		for(unsigned int i = 0; i < verticesFromOutput.size(); i++)
		{
			glVertex3f(verticesFromOutput[i].x,verticesFromOutput[i].y,verticesFromOutput[i].z);
		}
		glEnd();
	}


	//~ tempA = -3.138760;
	//~ tempB = -0.474452;
	//~ tempC = -55.338973;

    //~ glColor3f(1.0, 0.0, 0.0);
	//~ glBegin(GL_POINTS);	
	//~ for(int i = -500; i < 500; i++)
	//~ {
		//~ for(int j = -500; j < 500; j++)
		//~ {
				//~ double tempX = i;
				//~ double tempY = j;
				//~ double tempZ = ((tempB*tempY) + (tempC))+ (tempA*tempX);
				//~ glVertex3f(tempX,tempY,tempZ);
		//~ }
	//~ }
	//~ glEnd();
	
	
	//~ tempA = -0.141230;
	//~ tempB = 1.551832;
	//~ tempC = -206.289604;
	
	//~ glColor3f(0.0, 1.0, 0.0);
	//~ glBegin(GL_POINTS);	
	//~ for(int i = -500; i < 500; i++)
	//~ {
		//~ for(int j = -500; j < 500; j++)
		//~ {
				//~ double tempX = i;
				//~ double tempY = j;
				//~ double tempZ = ((tempB*tempY) + (tempC))+ (tempA*tempX);
				//~ glVertex3f(tempX,tempY,tempZ);
		//~ }
	//~ }
	//~ glEnd();
	
//~ Intersection Point: 1205.238886, -298.560251, 126.912799
/*if(displayOutput)
{
	//~ tempA = 1205.238886;
	//~ tempB = -298.560251;
	//~ tempC = 126.912799;
	
	//~ Plane 12 Coefficients:
	//~ tempA = -1.194563;
	//~ tempB = 1.165450;
	//~ tempC = 46.204585;
	//~ tempA= 52.000476;
	//~ tempB= -159.346186;
	//~ tempC= 4.183681;
	
	//~ Plane 12 Coefficients:
	//~ A: 15.600143
	//~ B: -47.803856
	//~ C: 1.255104

//~ Plane 12 Coefficients:
	//~ A: -0.180189
	//~ B: 0.724551
	//~ C: -0.000001

	//~ Plane 12 Coefficients:
	//~ tempA= 1.634331;
	//~ tempB= -1.180955;
	//~ tempC= 0.012467;

//~ Point: 71.148923, -39.170515, 27.678848
//~ Tangent Plane Coefficients:
//~ Plane 12 Coefficients:
	//~ A: -12.429331
	//~ B: 38.087468
	//~ C: -1240.863737

	tempA= 0.482970;
	tempB= -3.141349;
	tempC= -129.729599;

	//~ tempA= -0.177098;
	//~ tempB= 0.714799;
	//~ tempC= 0.328346;


	
	//~ tempA= 0.482970;
	//~ tempB= -3.141349;
	//~ tempC= -129.729599;
	
	glColor3f(1.0, 0.0, 0.0);
	if(one)
	//~ glBegin(GL_POINTS);
	glBegin(GL_POLYGON);	
	for(int i = -500; i < 500; i++)
	{
		for(int j = -500; j < 500; j++)
		{
				double tempX = i;
				double tempY = j;
				double tempZ = ((tempB*tempY) + (tempC))+ (tempA*tempX);
				glVertex3f(tempX,tempY,tempZ);
		}
	}
	glEnd();
	
	

//~ Intersection Point: 177.540815, -75.372396, 128.491186

	//~ tempA = 177.540815;
	//~ tempB = -75.372396;
	//~ tempC = 128.491186;
//~ Plane 13 Coefficients:
	//~ tempA= 4.247152;
	//~ tempB= 1.441677;
	//~ tempC= 334.245159;

//~ Plane 13 Coefficients:
	//~ A: 8.318353
	//~ B: -18.430085
	//~ C: 9.706861
	
//~ Plane 13 Coefficients:
	//~ A: 0.139756
	//~ B: -0.308913
	//~ C: -0.000650

//~ Plane 13 Coefficients:
	//~ tempA= 0.717626;
	//~ tempB= 0.133206;
	//~ tempC= -0.139095;

//~ Point: 27.244276, 137.850170, 26.090241
//~ Tangent Plane Coefficients:
	//~ tempA= -1.278376;
	//~ tempB= 2.482607;
	//~ tempC= -281.297686;
	
	//~ Plane 13 Coefficients:
	//~ A: -0.856956
	//~ B: 1.898666
	//~ C: -2.815854
	//~ A: 0.033381
	//~ B: -0.245667
	//~ C: 15.815974
	//~ A: 2.481740
	//~ B: -8.106583
	//~ C: -466.433326

	//~ A: 0.033381
	//~ B: -0.245667
	//~ C: 15.815974

	//~ A: 0.484761
	//~ B: -3.132031
	//~ C: -129.490897

		//~ A: 0.480924
	//~ B: -3.135433
	//~ C: -129.349441

	tempA= 0.480924;
	tempB= -3.135433;
	tempC= -129.730906;
	
//~ Point: 71.148923, -39.170515, 27.678848
//~ Tangent Plane Coefficients:
	//~ A: 2.481758
	//~ B: -8.106631
	//~ C: -466.436445



	
		//~ Point: 71.148923, -39.170515, 27.678848
//~ Tangent Plane Coefficients:
	//~ A: -0.287232
	//~ B: -1.228376
	//~ C: -0.000000

//~ Point: 27.244276, 137.850170, 26.090241
//~ Tangent Plane Coefficients:
	//~ A: -0.499231
	//~ B: 0.287951
	//~ C: 0.000000

//~ Point: 52.872882, 97.715111, -28.435166
//~ Tangent Plane Coefficients:
	//~ A: -0.159555
	//~ B: -0.201993
	//~ C: -0.001946

//~ Point: 110.520427, 6.858625, 79.040580
//~ Tangent Plane Coefficients:
	//~ A: 0.731093
	//~ B: -0.201029
	//~ C: 0.003215
	
	//~ Point: 27.244276, 137.850170, 26.090241
//~ Tangent Plane Coefficients:
	//~ A: -1.278376
	//~ B: 2.482607
	//~ C: -281.297686


	
//~ Point: 27.244276, 137.850170, 26.090241
//~ Tangent Plane Coefficients:
	//~ A: 0.328079
	//~ B: 0.632533
	//~ C: 0.154042



	//~ tempA= -1.278376;
	//~ tempB= 2.482607;
	//~ tempC= -281.297686;

	
	glColor3f(0.0, 1.0, 0.0);
	if(two)
	//~ glBegin(GL_POINTS);
	glBegin(GL_POLYGON);	
	for(int i = -500; i < 500; i++)
	{
		for(int j = -500; j < 500; j++)
		{
				double tempX = i;
				double tempY = j;
				double tempZ = ((tempB*tempY) + (tempC)) +(tempA*tempX);
				glVertex3f(tempX,tempY,tempZ);
		}
	}
	glEnd();
	

//~ Intersection Point: -617.790800, -61.185885, 125.873767
	
	//~ tempA = -617.790800;
	//~ tempB = -61.185885;
	//~ tempC = 125.873767;
//~ Plane 23 Coefficients:
//~ x: 52.000476
//~ y: -159.346186
//~ z: 4.183681

//~ Plane 23 Coefficients:
	//~ A: 0.035413
	//~ B: -3.148648
	//~ C: -3.934679
	
//~ Plane 23 Coefficients:
	//~ A: -0.020392
	//~ B: -0.003023
	//~ C: 0.000016

//~ Plane 23 Coefficients:
	//~ tempA= 1.753855;
	//~ tempB= 0.398289;
	//~ tempC= -0.678432;
	
//~ Point: 52.872882, 97.715111, -28.435166
//~ Tangent Plane Coefficients:
	//~ A: 0.009000
	//~ B: -0.800230
	//~ C: 92.720191

	tempA= -0.451894;
	tempB= 0.375060;
	tempC= -41.182114;
	
	
//~ Point: 52.872882, 97.715111, -28.435166
//~ Tangent Plane Coefficients:
	//~ A: -0.451894
	//~ B: 0.375060
	//~ C: -41.182114

	
//~ Point: 52.872882, 97.715111, -28.435166
//~ Tangent Plane Coefficients:
	//~ A: 0.295206
	//~ B: 0.695995
	//~ C: -0.571972


	//~ tempA= -0.022537;
	//~ tempB= -0.005087;
	//~ tempC= 0.307746;
	//~ tempA = 1205.721856;
	//~ tempB = 301.701600;
	//~ tempC = -2.816801;

	
	glColor3f(0.7, 0.3, 0.5);
	if(three)
	//~ glBegin(GL_POINTS);
	glBegin(GL_POLYGON);
	for(int i = -500; i < 500; i++)
	{
		for(int j = -500; j < 500; j++)
		{
				double tempX = i;
				double tempY = j;
				double tempZ = ((tempB*tempY) + (tempC))+ (tempA*tempX);
				glVertex3f(tempX,tempY,tempZ);
		}
	}
	//~ for(double i = -500.0; i <= 500.0; i+=0.01)
	//~ {
		//~ glVertex3f(tempX1 + i*tempA,tempY1+i*tempB,tempZ1+i*tempC);
		//~ glVertex3f(tempA,tempB,tempC);
	//~ }
	glEnd();

//~ Plane 14 Coefficients:
	//~ A: -0.774504
	//~ B: -0.539511
	//~ C: 114.995261

	tempA = -1.317107;
	tempB = 0.583188;
	tempC = 220.594581;


//~ Point: 110.520427, 6.858625, 79.040580
//~ Tangent Plane Coefficients:
	//~ A: -1.317107
	//~ B: 0.583188
	//~ C: 220.594581

//~ Point: 110.520427, 6.858625, 79.040580
//~ Tangent Plane Coefficients:
	//~ A: 0.689652
	//~ B: -0.013303
	//~ C: 0.301807
	
	glColor3f(0.3, 0.7, 0.5);
	if(four)
	{
	glBegin(GL_POLYGON);
	for(int i = -500; i < 500; i++)
	{
		for(int j = -500; j < 500; j++)
		{
				double tempX = i;
				double tempY = j;
				double tempZ = ((tempB*tempY) + (tempC))+ (tempA*tempX);
				glVertex3f(tempX,tempY,tempZ);
		}
	}
	//~ for(double i = -500.0; i <= 500.0; i+=0.01)
	//~ {
		//~ glVertex3f(tempX1 + i*tempA,tempY1+i*tempB,tempZ1+i*tempC);
		//~ glVertex3f(tempA,tempB,tempC);
	//~ }
	glEnd();
	}


//~ Intersection Point 1: -10.413059, 41.825778, 25.413909

//~ Intersection Point 2: 1195.308797, 343.527378, 22.597108
	
	 //~ 10.400673, -41.828877, -25.413880
	//~ 199.767790, -198.849243, -3.735347
//~ -199.767790, 198.849243, 3.735347

//~ Point 1:
//~ x: 71.148923
//~ y: -39.170515
//~ z: 27.678848

//~ Point 2:
//~ x: 27.244276
//~ y: 137.850170
//~ z: 26.090241

//~ Midpoint:
//~ x: 49.196599
//~ y: 49.339827
//~ z: 26.884544

//~ Centroid:
//~ x: 117.230262
//~ y: 71.643571
//~ z: 30.764743

//~ Direction vector:
//~ x: 0.599236
//~ y: 0.187673
//~ z: -0.300132

//~ Point 1 for midpoint:
double tempx1= 136.705824;
double tempy1= 103.795973;
double tempz1= 14.027046;

//~ Point 2 for midpoint:
double tempx2= 81.809314;
double tempy2= 135.590338;
double tempz2= 60.419342;

//~ x: 49.196599
//~ y: 49.339827
//~ z: 26.884544

//~ Midpoint 13:
//~ x: 62.010903
//~ y: 29.272298
//~ z: -0.378159

//~ Midpoint 23:
//~ x: 40.058579
//~ y: 117.782640
//~ z: -1.172462

//~ Point 1:
//~ x: 71.148923
//~ y: -39.170515
//~ z: 27.678848

//~ Midpoint 13:
//~ x: 62.010903
//~ y: 29.272298
//~ z: -0.378159

//~ Point 3:
//~ x: 52.872882
//~ y: 97.715111
//~ z: -28.435166


	double tempX1 = 71.148923;
	double tempY1 = -39.170515;
	double tempZ1 = 27.678848;
	
	double tempX2 = 62.010903;
	double tempY2 = 29.272298;
	double tempZ2 = -0.378159;
	
	//~ Point 2...AGA
//~ x: 274.535128
//~ y: 122.103982
//~ z: -2.520166

//~ Point 2...AGAIN!:
//~ x: 163.392116
//~ y: 86.554601
//~ z: 24.927190

//~ Point 2...AGAIN!:
//~ x: -240.764292
//~ y: -42.715875
//~ z: 124.735758
//~ Point 2...AGAIN!:
//~ x: 48.704190
//~ y: 90.448111
//~ z: -1.027975
//~ x: 274.535128
//~ y: 122.103982
//~ z: -2.520166



	
	double tempX3 = 274.535128;
	double tempY3 = 122.103982;
	double tempZ3 = -2.520166;
	
	double tempX4 = 264.431218;
	double tempY4 = 118.872220;
	double tempZ4 = -0.024952;
	
	double tempX5 = 0.101039;
	double tempY5 = 0.032318;
	double tempZ5 = -0.024952;
	
	//~ Centroid:
//~ x: 38.055010
//~ y: 8.358006
//~ z: -0.801298
//~ x: -5.226634
//~ y: -1.147748
//~ z: 0.110171
//~ Centroid:
//~ x: -2012.596798
//~ y: -3699.202215
//~ z: -0.513987

//~ Centroid:
//~ x: 264.489084
//~ y: 118.890729
//~ z: -0.039242

//~ Centroid:
//~ x: 151.541633
//~ y: 68.110814
//~ z: -0.039242

//~ x.center: 49.8325
//~ y.center: 50.6754
//~ z.center: 50.1578

	
	tempA = 49.8325;
	tempB = 50.6754;
	tempC = 50.1578;
	
	//~ tempA = 151.541633;
	//~ tempB = 68.110814;
	//~ tempC = -0.039242;
	
	
	
	if(five)
	{
	glPointSize(5.0);
	glBegin(GL_POINTS);
	//~ glBegin(GL_LINES);
	//~ for(double i = -500.0; i <= 500.0; i+=0.01)
	//~ {
		//~ //glVertex3f(tempX1 + i*tempA,tempY1+i*tempB,tempZ1+i*tempC);
		//~ glColor3f(0.0, 1.0, 1.0);
		//~ glVertex3f(tempX1,tempY1,tempZ1);
		//~ glColor3f(0.0, 1.0, 1.0);		
		//~ glVertex3f(tempX2,tempY2,tempZ2);
		//~ glColor3f(0.0, 1.0, 1.0);
		//~ glVertex3f(tempX3,tempY3,tempZ3);
		//~ glColor3f(0.2, 0.3, 0.7);
		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(tempA,tempB,tempC);
		//~ glColor3f(1.0, 0.0, 0.0);
		//~ glVertex3f(tempX3,tempY3,tempZ3);
		//~ glColor3f(0.0, 0.2, 1.0);
		//~ glVertex3f(tempX4,tempY4,tempZ4);
		//~ glColor3f(0.6, 0.0, 1.0);
		//~ glVertex3f(tempX5,tempY5,tempZ5);
		glColor3f(0.6, 0.6, 1.0);
		glVertex3f(50,50,50);
	}
	glEnd();
	glPointSize(1.0);
	//~ glBegin(GL_LINES);
	//for(double i = -500.0; i <= 500.0; i+=0.01)
	//{
		//glVertex3f(tempX1 + i*tempA,tempY1+i*tempB,tempZ1+i*tempC);
		//~ glColor3f(1.0, 0.0, 1.0);
		//~ glVertex3f(tempx1,tempy1,tempz1);
		//~ glColor3f(1.0, 0.0, 1.0);		
		//~ glVertex3f(tempX1,tempY1,tempZ1);
		//~ glColor3f(1.0, 0.0, 1.0);
		//~ glVertex3f(tempX1,tempY1,tempZ1);
		//~ glColor3f(0.2, 0.3, 0.7);
		//~ glVertex3f(tempx2,tempy2,tempz2);
		//glColor3f(0.0, 0.3, 0.6);
		//glVertex3f(tempA,tempB,tempC);
		//glVertex3f(tempX5,tempY5,tempZ5);
	//}
	//~ glEnd();
	//~ }
	//~ glColor3f(1.0, 0.0, 1.0);
	//~ if(five)
	//~ glBegin(GL_LINES);
	//~ for(double i = -500.0; i <= 500.0; i+=0.01)
	//~ {
		//~ glVertex3f(tempX1 + i*tempA,tempY1+i*tempB,tempZ1+i*tempC);
		//~ glVertex3f(tempX1,tempY1,tempZ1);
		//~ glVertex3f(tempA,tempB,tempC);
	//~ }
	//~ glEnd();

//~ Intersection Point 2: 1184.581105, -217.588011, -47.809886
	
	//~ X0 vector:
//~ x: -427.447334
//~ y: -106.920227
//~ z: -0.300132

//~ Direction vector:
//~ x: 0.599236
//~ y: 0.187673
//~ z: -0.300132

//~ Point 1:
//~ x: 38.056288
//~ y: 8.358327
//~ z: -0.801298

//~ Point 2:
//~ x: 38.055537
//~ y: 8.358138
//~ z: -0.801298

//~ Point 1:
//~ tempA= 38.056288;
//~ tempB= 8.358327;
//~ tempC= -0.801298;

//~ Point 2:
//~ tempX1= 38.055537;
//~ tempY1= 8.358138;
//~ tempZ1= -0.801298;

//~ Point 1:
//~ tempA= 48.726000;
//~ tempB= 90.029445;
//~ tempC= -0.513987;

//~ Point 2:
//~ tempX1= -0.021810;
//~ tempY1= 0.418666;
//~ tempZ1= -0.513987;

//~ Point 1:
//~ tempA= 0.034740;
//~ tempB= 0.434858;
//~ tempC= -0.444304;

//~ Point 2:
//~ tempX1= -0.257760;
//~ tempY1= 0.019508;
//~ tempZ1= -0.444304;
//~ Intersection Point 1: 264.431218, 118.872220, -0.024952

//~ Intersection Point 2: 0.101039, 0.032318, -0.024952

		
	tempX1 = 264.431218;
	tempY1 = 118.872220;
	tempZ1 = -0.024952;
	

	tempA = 0.101039;
	tempB = 0.032318;
	tempC = -0.024952;
	
//~ Intersection Point 1: 102.511151, 66.031793, -0.024952

//~ Intersection Point 2: 0.026622, 0.008032, -0.024952

	
	glColor3f(1.0, 1.0, 0.0);
	if(five)
	glBegin(GL_POINTS);
	//glPointSize(5.0);
	for(double i = -5000.0; i <= 5000.0; i+=0.01)
	{
		glVertex3f(tempX1 + i*tempA,tempY1+i*tempB,tempZ1+i*tempC);
		//~ glVertex3f(tempA,tempB,tempC);
	}
	glEnd();
	glPointSize(1.0);
	
	//~ tempA = 712.977515;
	//~ tempB = -654.984741;
	//~ tempC = 283.822013;

	
	//~ glColor3f(0.0, 0.5, 0.7);
	//~ if(five)
	//~ glBegin(GL_POLYGON);
	//~ for(int i = -500; i < 500; i++)
	//~ {
		//~ for(int j = -500; j < 500; j++)
		//~ {
				//~ double tempX = i;
				//~ double tempY = j;
				//~ double tempZ = ((tempB*tempY) + (tempC))+ (tempA*tempX);
				//~ glVertex3f(tempX,tempY,tempZ);
		//~ }
	//~ }
	//~ glEnd();
	

//~ Intersection Point: 107.205026, -154.301676, 41.412569
	
	//~ tempA = 107.205026;
	//~ tempB = -154.301676;
	//~ tempC = 41.412569;

	
	//~ glColor3f(0.5, 0.0, 0.7);
	//~ if(six)
	//~ glBegin(GL_POLYGON);	
	//~ for(int i = -500; i < 500; i++)
	//~ {
		//~ for(int j = -500; j < 500; j++)
		//~ {
				//~ double tempX = i;
				//~ double tempY = j;
				//~ double tempZ = ((tempB*tempY) + (tempC))+ (tempA*tempX);
				//~ glVertex3f(tempX,tempY,tempZ);
		//~ }
	//~ }
	//~ glEnd();

	//~ glColor3f(0.0, 0.5, 0.7);
	//~ glBegin(GL_POINTS);	
	//~ for(int i = 0; i < 500; i++)
	//~ {
		//~ for(int j = 0; j < 500; j++)
		//~ {
				//~ double tempX = i;
				//~ double tempY = j;
				//~ double tempZ = ((1.551832*tempY) + (-206.289604))+ (-0.141230*tempX);
				//~ glVertex3f(tempX,tempY,tempZ);
		//~ }
	//~ }
	//~ glEnd();

}
* */
	if(six)
	{
		glColor3f(1.0, 1.0, 1.0);
		glBegin(GL_POINTS);
		for(unsigned int i = 0; i < vertices.size(); i++)
		{
			//glColor3f(vertices[i].r,vertices[i].g,vertices[i].b);
			glVertex3f(vertices[i].x,vertices[i].y,vertices[i].z);
		}
		glEnd();
	}	
	
	
	glLoadIdentity();

	if(autoRotate)
	{
		angle += 0.5;

		if (angle >= 360.0)
			angle = 0.0;
	}

	glutSwapBuffers();
}// End display function

// Begin keyboard function
void keyboard_func(unsigned char c, int x, int y)
{
	if(c == 'r')
	{
		if(angle >= 360.0)
			angle = 0.0;
		if (angle < 360.0)
		{
			angle += 0.5;
		}
	}
	if(c == 't')
	{
		if(angle <= 0.0)
			angle = 360.0;
		if (angle > 0.0)
		{
			angle -= 0.5;
		}
	}
	if(c == 'a')
		autoRotate = !autoRotate;
	if(c == 'k')
	{
		near_y += 100.0;
		far_y += 100.0;
	}
	if(c == 'i')
	{
		near_y -= 100.0;
		far_y -= 100.0;
	}
	if(c == 'j')
	{
		near_x += 100.0;
		far_x += 100.0;
	}
	if(c == 'l')
	{
		near_x -= 100.0;
		far_x -= 100.0;
	}

	if(c == 'z')
	{
		near_x *= 0.75;
		far_x *= 0.75;
		near_y *= 0.75;
		far_y *= 0.75;
	}
	if(c == 'Z')
	{
		near_x *= 1.25;
		far_x *= 1.25;
		near_y *= 1.25;
		far_y *= 1.25;
	}
	if(c == 'o')
		displayOutput = !displayOutput;
		
	//~ if(c == 'x')
		//~ one = !one;
	//~ if(c == 'c')
		//~ two = !two;
	//~ if(c == 'v')
		//~ three = !three;
	//~ if(c == 'b')
		//~ four = !four;
	//~ if(c == 'n')
		//~ five = !five;
	if(c == 'm')
		six = !six;
		
	
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(near_x, far_x, near_y, far_y, near_z, far_z);
	
	glutPostRedisplay();
}// End keyboard function


void mouse_func(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON) {
        if(state == GLUT_DOWN) {
            dragging = 1;
            drag_x_origin = x;
            drag_y_origin = y;
        }
        else
            dragging = 0;
    }
}

void mouse_move(int x, int y) {
    if(dragging) {
        camera_angle_v += (y - drag_y_origin)*0.3;
        if(camera_angle_v >= 90.0)
			camera_angle_v = 90.0;
        else if(camera_angle_v <= -90.0)
			camera_angle_v = -90.0;
        camera_angle_h += (x - drag_x_origin)*0.3;
        if(camera_angle_h >= 180.0)
			camera_angle_h = 180.0;
        else if(camera_angle_h <= -180.0)
			camera_angle_h = -180.0;
        drag_x_origin = x;
        drag_y_origin = y;
    }
}


void reshape_handler(int width, int height)
{
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(near_x, far_x, near_y, far_y, near_z, far_z);
}

void init_setup(int width, int height, char *windowName)
{
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(width, height);
	glutInitWindowPosition(5, 5);
	glutCreateWindow(windowName);
	glutReshapeFunc(reshape_handler);
}

bool readFile(string fileName, vector<MyPoint> &verts)
{
	ifstream inFile(fileName.c_str());
	if(!inFile)
		return false;
	
	string tempStr;
	double temp;
	inFile >> tempStr;
	if(tempStr == "ply")
	{
		while(tempStr != "vertex")
		{
			inFile >> tempStr;
		}
		
		
		inFile >> numVertices;
		cout << numVertices << endl;
		
		while(tempStr != "end_header")
		{
			inFile >> tempStr;
		}
		double min_x, max_x, min_y, max_y, min_z, max_z;
		
		MyPoint tempPt;
		inFile >> temp;
		tempPt.x = temp;
		min_x = temp;
		max_x = temp;

		inFile >> temp;
		tempPt.y = temp;
		min_y = temp;
		max_y = temp;

		inFile >> temp;
		tempPt.z = temp;
		min_z = temp;
		max_z = temp;
		
		inFile >> temp;
		tempPt.r = temp/255.0;

		inFile >> temp;
		tempPt.g = temp/255.0;

		inFile >> temp;
		tempPt.b = temp/255.0;
		
		verts.push_back(tempPt);
		
		for(int i = 1; i < numVertices; i++)
		{
			inFile >> temp;
			tempPt.x = temp;
			if(temp > max_x)
				max_x = temp;
			else if(temp < min_x)
				min_x = temp;

			inFile >> temp;
			tempPt.y = temp;
			if(temp > max_y)
				max_y = temp;
			else if(temp < min_y)
				min_y = temp;

			inFile >> temp;
			tempPt.z = temp;
			if(temp > max_z)
				max_z = temp;
			else if(temp < min_z)
				min_z = temp;
			
			inFile >> temp;
			tempPt.r = temp/255.0;

			inFile >> temp;
			tempPt.g = temp/255.0;

			inFile >> temp;
			tempPt.b = temp/255.0;
			
			verts.push_back(tempPt);
		}
		
		cout << max_x << ", " << min_x << endl;
		cout << max_y << ", " << min_y << endl;
		cout << max_z << ", " << min_z << endl << endl;;
		
		center_x = (max_x - min_x)/2.0;
		center_y = (max_y - min_y)/4.0;
		center_z = (max_z - min_z)/2.0;
		
		cout << center_x << endl;
		cout << center_y << endl;
		cout << center_z << endl;
	}
	else
	{
		temp = atof(tempStr.c_str());
		if(temp == 4.0)
		{
			inFile >> center_x >> center_y >> center_z >> rad >> numVertices;
			//isSphere = true;
		}
		else if(temp == 9.0)
		{
			inFile >> center_x >> center_y >> center_z >> axis_a >> axis_b >> axis_c >> alpha >> beta >> theta >> numVertices;
			//isSphere = false;
		}
		else
		{
			cout << "Invalid shape. Exiting..." << endl;
			return false;
		}
		
		while(!inFile.eof())
		{
			MyPoint tempPt;
			inFile >> temp;
			tempPt.x = temp;

			inFile >> temp;
			tempPt.y = temp;

			inFile >> temp;
			tempPt.z = temp;
			
			tempPt.r = 1.0;
			tempPt.g = 1.0;
			tempPt.b = 1.0;
			
			verts.push_back(tempPt);
		}
	}

	return true;
}
